package course13;

public class Mistake5 {
    public static void test1(int x) throws ArrayIndexOutOfBoundsException, Exception {
        System.out.println(x);
        if (x == 0) {
            System.out.println("没有异常");
        }
        //数组越界处理
        else if (x == 1) {

                int[] a = new int[3];
                a[3] = 5;
        }
        //算数异常
        else if (x==2){
                int i=0;
                int j=5/0;

        }
    }

    public static void main(String[] args) {

        //创建对象

        // 调用会抛出异常的方法，用try-catch块
        try {

            test1(0);

        } catch (Exception e) {

            System.out.println(e);
        }

        // 数组越界异常


        try {
            test1(1);
        } catch (Exception e) {
            System.out.println("数组越界异常:" + e);
        }


        // 算术异常

        try {
            test1(2);
        } catch (Exception e) {
            System.out.println("算术异常:" + e);
        }
        //使用 throw 抛出异常(可以抛出异常对象，也可以抛出异常对象的引用)
        try{

            ArrayIndexOutOfBoundsException  exception = new ArrayIndexOutOfBoundsException();

            throw exception;//new ArrayIndexOutOfBoundsException();

        }catch(ArrayIndexOutOfBoundsException e){

            System.out.println("thorw抛出异常:"+e);
        }

    }
}
